<template>
  <!-- 角色表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    style="min-width: 500px; max-width: 800px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="分组编号" prop="routingGroupCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.routingGroupCode"
              placeholder="请输入分组编号"
              clearable
            />
            <span v-else>{{ formData.routingGroupCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分组简述" prop="routingGroupName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.routingGroupName"
              placeholder="请输入分组简述"
              clearable
            />
            <span v-else>{{ formData.routingGroupName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="分组流水号" prop="routingGroupSerial">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.routingGroupSerial"
              placeholder="请输入分组流水号"
              clearable
            />
            <span v-else>{{ formData.routingGroupSerial }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用" prop="enabled">
            <el-radio-group v-model="formData.enabled" v-if="!props.isReadonly">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
            <span v-else>{{
              dictionaryStore.filterDictName("Enabled", formData.enabled)
            }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-show="props.isReadonly">
        <el-col :span="12">
          <el-form-item
            label="创建时间"
            v-if="formData.id && !props.isReadonly"
          >
            <el-input v-model="formData.createTime" disabled />
          </el-form-item>
          <el-form-item v-if="props.isReadonly" label="创建时间">
            <span>{{ formData.createTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建人" v-if="formData.id && !props.isReadonly">
            <el-input v-model="formData.createdUserName" disabled />
          </el-form-item>
          <el-form-item v-if="props.isReadonly" label="创建人">
            <span>{{ formData.createdUserName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="props.isReadonly">
        <el-col :span="12">
          <el-form-item
            label="更新时间"
            v-if="formData.id && !props.isReadonly"
          >
            <el-input v-model="formData.lastUpdateTime" disabled />
          </el-form-item>
          <el-form-item v-if="props.isReadonly" label="更新时间">
            <span>{{ formData.lastUpdateTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人" v-if="formData.id && !props.isReadonly">
            <el-input v-model="formData.lastUpdatedUserName" disabled />
          </el-form-item>
          <el-form-item v-if="props.isReadonly" label="更新人">
            <span>{{ formData.lastUpdatedUserName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESROUTINGGROUPADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  routingGroupCode: [
    { required: true, message: "请输入分组编号", trigger: "blur" },
  ],
  routingGroupName: [
    { required: true, message: "请输入分组简述", trigger: "blur" },
  ],
  routingGroupSerial: [
    { required: true, message: "请输入分组流水号", trigger: "blur" },
  ],
  plantId: [{ required: true, message: "请输入工厂id", trigger: "blur" }],
  plantCode: [{ required: true, message: "请输入工厂编号", trigger: "blur" }],
  createTime: [{ required: true, message: "请输入创建时间", trigger: "blur" }],
  createdUserName: [
    { required: true, message: "请输入创建人名", trigger: "blur" },
  ],
  lastUpdateTime: [
    { required: true, message: "请输入更新时间", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { required: true, message: "请输入更新人名", trigger: "blur" },
  ],
  enabled: [{ required: true, message: "请输入是否启用", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  routingGroupCode: "",
  routingGroupName: "",
  routingGroupSerial: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: 1,
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        routingGroupCode,
        routingGroupName,
        routingGroupSerial,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        routingGroupCode,
        routingGroupName,
        routingGroupSerial,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.routingGroupCode = "";
  formData.routingGroupName = "";
  formData.routingGroupSerial = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = 1;
  formData.deleted = "";
}

const loading = ref(false);

/** 工艺路线分组保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesroutinggroup/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

/** 工艺路线分组查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/mes/mesroutinggroup/getDetails",
    method: "post",
    data: formData,
  });
  console.log(data);
}

onMounted(() => {
  if (formData.id) {
    getDatails();
  }
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
